package command.java;

import java.util.List;
import java.util.ArrayList;

/* The Command interface */
public interface Command {
    void execute();
}

/* The Invoker class */
class Switch {
    private List<Command> commands = new ArrayList<Command>();

    public Switch() {
    }

    public void store(Command cmd) {
        this.commands.add(cmd);
    }

    public void execute() {
        for (Command cmd : commands)
            cmd.execute();
    }
}

/* The Receiver class */
class Light {
    public Light() {
    }

    public void turnOn() {
        System.out.println("The light is on");
    }

    public void turnOff() {
        System.out.println("The light is off");
    }
}

/* The Command for turning on the light - ConcreteCommand #1 */
class FlipUpCommand implements Command {
    private Light theLight;

    public FlipUpCommand(Light light) {
        this.theLight = light;
    }

    public void execute() {
        theLight.turnOn();
    }
}

/* The Command for turning off the light - ConcreteCommand #2 */
class FlipDownCommand implements Command {
    private Light theLight;

    public FlipDownCommand(Light light) {
        this.theLight = light;
    }

    public void execute() {
        theLight.turnOff();
    }
}

/* The test class or client */
class PressSwitch {
    public static void main(String[] args) {
        Light lamp = new Light();
        Command switchUp = new FlipUpCommand(lamp);
        Command switchDown = new FlipDownCommand(lamp);

        Switch s = new Switch();

        s.store(switchUp);
        s.store(switchDown);
        s.execute();
    }
}